package com.wisdudu.blank.data.source.remote.service;

import com.wisdudu.blank.data.source.remote.client.retrofit.client.RetrofitClient;

import org.json.JSONObject;

import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by qyf on 2016/9/12.
 */

public enum CqService {
    INSTANCE;

    private CqAPI mTestAPI;


    CqService() {
        mTestAPI = RetrofitClient.INSTANCE
                .getRetrofitBuilder()
                .baseUrl(CqAPI.BASE_URL).build()
                .create(CqAPI.class);
    }

    public CqAPI getApi() {
        return mTestAPI;
    }


    public String getKey(Map<String, Object> params) {
        try {
            String key = "9ce62c1836d128cfc875c9026db7564c";
//            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
            //初始化，设置为加密模式
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
            byte[] data = cipher.doFinal(new JSONObject(params).toString().getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte aData : data) {
                String hex = Integer.toHexString(aData & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                sb.append(hex);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
